<?php

namespace app\system\controller;

use app\common\controller\Excel;
use app\common\controller\Ztpay;
use think\Db;
use think\Exception;

class Withdraw extends Common
{
    protected $table = 'draw', $name = '提币申请';

    function select()
    {
        $where = ['t.is_del' => '0'];
        if (input('keyword')) $where['t.code|t.nickname'] = ['like', '%' . input('keyword') . '%'];
        if(input('start')){
            $value = explode(' - ',input('start'));
            $where['create_time'] = array('between',''.strtotime($value[0]).','.strtotime($value[1]).'');
        }
        if (input('state')) $where['t.state'] = input('state');
        if (input('way')) $where['t.way'] = input('way');

        $list = db($this->table . ' t')->where($where)->order('t.id desc');

        return $list;
    }

    function index()
    {
        $list = $this->select()->paginate(300, false, ['query' => request()->get()]);
        $this->assign('list', $list);

        return $this->fetch();
    }

    function add($id = '')
    {
        if ($id) {
            $info = db($this->table)->find($id);
            $this->assign('info', $info);
        }

        return $this->fetch();
    }

    function save($id = '')
    {
        $info = db($this->table)->find($id);
        if ($info['state'] != 1) return $this->jsonError('请勿重复处理');
        
        if (request()->isPost()) {
            Db::startTrans();
            try {
                //数据
                $data = input('post.');
                $data['confirm_time'] = time();
                $data['admin_id'] = $this->admin['id'];
                $data['admin_name'] = $this->admin['name'];

                //修改
                $res = db($this->table)->where(['id' => $id])->update($data);
                $action = '审核';
                if ($res) {
                    //驳回后返还
                    if (input('state') == 3) {
                        // if($info['type'] == '奖金'){
                        //     $type = 'bonus';
                        // }else{
                        //     $type = 'coins';
                        // }
                        // echo $type;die;
                        \app\common\model\Account::add($info['uid'],$info['type'],$info['total'],'提现驳回','+');
                    }

                    //整体成功
                    Db::commit();
                    action('common/Logs/addLog', ['管理员[' . $this->admin['name'] . ']' . $action . '了' . $this->name . '，' . $this->name . 'ID[' . $id . ']']);
                    return $this->jsonSuccess('审核成功');;
                } else {
                    Db::rollback();
                    return $this->jsonError('审核失败:error(3)');
                }
            } catch (Exception $e) {
                Db::rollback();
                return $this->jsonError('审核失败:error(2)');
            }
        } else {
            return $this->jsonError('审核失败:error(1)');
        }
    }

    function state($id, $key, $val)
    {
        return $this->editState($this->table, $id, $key, $val);
    }

    function value($id, $key, $val)
    {
        return $this->editValue($this->table, $id, $key, $val);
    }

    function export()
    {
        $excelc = new Excel();

        $map = [
            ['title' => '用户编号', 'key' => 'code'],
            ['title' => '姓名', 'key' => 'nickname'],
            ['title' => '申请时间', 'key' => 'create_time'],
            ['title' => '提现类型', 'key' => 'type'],
            ['title' => '金额', 'key' => 'total'],
            ['title' => '手续费', 'key' => 'cost'],
            ['title' => '实际金额', 'key' => 'amount'],
            ['title' => '银行名称', 'key' => 'bank_name'],
            ['title' => '银行户名', 'key' => 'bank_account'],
            ['title' => '银行账号', 'key' => 'bank_number'],
            ['title' => '状态', 'key' => 'state'],
        ];

        $data = $this->select()->select();
        $keys = ['未知','待审核','已通过','已驳回'];
        foreach ($data as $k => &$v) {
            $v['create_time'] = date('Y-m-d', $v['create_time']);
            $v['state'] = $keys[$v['state']];
            $bank = unserialize($v['info']);
            $v['bank_name'] = $bank['bank_name'];
            $v['bank_account'] = $bank['bank_account'];
            $v['bank_number'] = $bank['bank_number'] . ' ';
        }

        $excelc->export('提现记录' . date('YmdHis'), $map, $data);
    }



}